import httpUtil from "../utils/httpUtil"
import userApi from "./userApi"

const sendCode = (mobile, successCallBack, errCallBack) => {
	let params = {
		mobile,
	}
	httpUtil.post('/api/tongliansign/sendSms', params, res => {
		if (successCallBack) {
			successCallBack(res)
		}
	}, err => {
		if (errCallBack) {
			errCallBack(res)
		}
	})
}

const login = (mobile, captcha, successCallBack, errCallBack) => {

	let params = {
		mobile,
		captcha,
		type: 3
	}

	httpUtil.post('/api/tongliansign/checkSms', params, res => {
		setMobile(res.data.mobile)
		setOpenId(res.data.openid)

		if (successCallBack) {
			successCallBack(res)
		}
	}, err => {
		if (errCallBack) {
			errCallBack(res)
		}
	})
}

const setOpenId = (value) => {
	uni.setStorageSync('openId', value)
}

const getOpenId = () => {
	return uni.getStorageSync('openId')
}

const setMobile = (value) => {
	uni.setStorageSync('mobile', value)
}

const getMobile = () => {
	return uni.getStorageSync('mobile')
}

const removeMobile = () => {
	uni.removeStorageSync('mobile')
}


/**
 * 埋点
 */
const buriedPoint = (env = 5) => {
	// if (!userApi.getUserId()) {
	// 	console.log('未能获取到openid跳过')
	// 	return;
	// }
	let params = {
		a: httpUtil.getA(),
		// openid: getOpenId(),
		openid: userApi.getUserId(),
		env,
	}
	httpUtil.postReturnSource('/api/rf/getMpUrl', params)
}

/**
 * 检查订单是否支付成功
 */
const checkOrderIsSuccess = (mobile, successCallBack, failCallBack) => {
	let params = {
		mobile,
	}
	httpUtil.post('/api/xmly/queryMobile', params, res => {
		if (successCallBack) {
			successCallBack(res)
		}
	}, err => {
		if (failCallBack) {
			failCallBack(err)
		}
	})
}

const setParam = (value) => {
	uni.setStorageSync('param', value)
}

const getParam = () => {
	if (uni.getStorageSync('param')) {
		return uni.getStorageSync('param')
	}
	return ''
}


const setIsSubmit = (value) => {
	uni.setStorageSync('isSubmit', value)
}

const getIsSubmit = () => {
	return uni.getStorageSync('isSubmit')
}

const removeIsSubmit = () => {
	uni.removeStorageSync('isSubmit')
}


export default {
	setIsSubmit,
	getIsSubmit,
	removeIsSubmit,
	setParam,
	getParam,
	checkOrderIsSuccess,
	buriedPoint,
	setMobile,
	getMobile,
	removeMobile,
	setOpenId,
	getOpenId,
	sendCode,
	login,
}